<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem :key="category.top.id" v-if="category.top" :to="`/category/${category.top.id}`">{{ category.top.name }}
    </XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem :key="category.sub.id" v-if="category.sub">{{ category.sub.name }}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>
<script setup>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed } from 'vue'
// 注意：setup中this不是当前vue实例

// 1. 获取二级分类的ID，在地址在路由中
// route ===> this.$route
const route = useRoute()
// 2. 获取vuex中的类目数据
// store ===> this.$store
const store = useStore()
// console.log('list', store.state.category.list)
// 3. 通过计算属性得到，二级类目的名称和ID，一级类目的名称和ID
// console.log(store.state.category)
const category = computed(() => {
  const cate = {}
  store.state.category.list.forEach(top => {
    // console.log('top', top)
    if (top.children) {
      const sub = top.children.find(sub => sub.id === route.params.id)
      if (sub) {
        cate.top = { id: top.id, name: top.name }
        cate.sub = { id: sub.id, name: sub.name }
      }
    }
  })
  return cate
})
</script>
<style scoped lang="less"></style>
